// Keywords: conditional sweep

initialize() {
	initializeMutationRate(1e-7);
	initializeMutationType("m1", 0.5, "f", 0.0);
	initializeMutationType("m2", 1.0, "f", 0.0);  // introduced mutation
	initializeGenomicElementType("g1", m1, 1.0);
	initializeGenomicElement(g1, 0, 99999);
	initializeRecombinationRate(1e-8);
}
1 early() {
	// save this run's identifier, used to save and restore
	defineConstant("simID", getSeed());
	
	sim.addSubpop("p1", 500);
}
1000 late() {
	// save the state of the simulation
	sim.outputFull(tempdir() + "slim_" + simID + ".txt");
	
	// introduce the sweep mutation
	target = sample(p1.genomes, 1);
	target.addNewDrawnMutation(m2, 10000);
}
1000: late() {
	mut = sim.mutationsOfType(m2);
	
	if (size(mut) == 1)
	{
		if (sim.mutationFrequencies(NULL, mut) > 0.1)
		{
			cat(simID + ": ESTABLISHED - CONVERTING TO BENEFICIAL\n");
			mut.setSelectionCoeff(0.5);
			community.deregisterScriptBlock(self);
		}
	}
	else
	{
		cat(simID + ": LOST BEFORE ESTABLISHMENT - RESTARTING\n");
		
		// go back to tick 1000
		sim.readFromPopulationFile(tempdir() + "slim_" + simID + ".txt");
		
		// start a newly seeded run
		setSeed(rdunif(1, 0, asInteger(2^62) - 1));
		
		// re-introduce the sweep mutation
		target = sample(p1.genomes, 1);
		target.addNewDrawnMutation(m2, 10000);
	}
}
1000:10000 late() {
	if (sim.countOfMutationsOfType(m2) == 0)
	{
		fixed = (sum(sim.substitutions.mutationType == m2) == 1);
		cat(simID + ifelse(fixed, ": FIXED\n", ": LOST\n"));
		sim.simulationFinished();
	}
}
